﻿
Imports DotNetNuke
Imports DotNetNuke.Security
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Common

Namespace effority.WF_HTML

    Partial Class EditHtml
        Inherits WF_HTML_Base

#Region " Private Members "

        Protected _isNew As Boolean = True

#End Region

#Region " Event Handlers "

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Try

                Dim objHTML As New HtmlTextController

                wf.UserLevel = Me.UserLevel
                wf.ModuleId = Me.ModuleId

                Dim objText As HtmlTextInfo

                If Not IsPostBack Then

                    If Me.UserLevel = UserLevelEnum.Normal Then

                        objText = objHTML.GetHtmlTextActiveVersion(ModuleId)

                    ElseIf Me.UserLevel = UserLevelEnum.Editor Or Me.UserLevel = UserLevelEnum.Workflow Then

                        objText = objHTML.GetHtmlTextLastVersion(ModuleId)

                    End If

                    If Not objText Is Nothing Then

                        Me.BindId(objText.Id)

                    Else

                        Me.BindId(DotNetNuke.Common.Utilities.Null.NullInteger)

                    End If

                End If


                cbUpdateLastChangeDate.Visible = Not Me.DisableDate


            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' cmdCancel_Click runs when the cancel button is clicked
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
            Try
                Response.Redirect(NavigateURL(), True)
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' cmdPreview_Click runs when the preview button is clicked
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub cmdPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPreview.Click

            Try

                Dim strDesktopHTML As String

                strDesktopHTML = teContent.Text

                lblPreview.Text = ManageUploadDirectory(Server.HtmlDecode(strDesktopHTML), PortalSettings.HomeDirectory)

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' cmdUpdate_Click runs when the update button is clicked
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click
            Try
                ' create HTMLText object
                Dim objHTML As New HtmlTextController
                Dim objText As HtmlTextInfo = New HtmlTextInfo

                ' set content values
                objText.ModuleId = ModuleId
                objText.DeskTopHTML = teContent.Text
                objText.CreatedByUser = Me.UserId

                objHTML.AddHtmlText(objText)

                If Not Me.DisableDate Then

                    Dim objModules As New Entities.Modules.ModuleController

                    If cbUpdateLastChangeDate.Checked Then

                        objModules.UpdateModuleSetting(ModuleId, Consts.LAST_UPDATED_DATE, CStr(Now.Ticks))

                    End If

                End If

                ' refresh cache
                SynchronizeModule()

                ' redirect back to portal
                Response.Redirect(NavigateURL(), True)

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        Private Sub wf_ActiveVersionChanged(ByVal sender As Object, ByVal e As Integer) Handles wf.ActiveVersionChanged

            Try

                Dim cont As New HtmlTextController

                cont.SetHtmlTextActive(e)

                Me.BindId(e)

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        Private Sub wf_ViewVersion(ByVal sender As Object, ByVal e As Integer) Handles wf.ViewVersion

            Try

                Dim cont As New HtmlTextController

                Me.BindId(e)

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        Private Sub wf_DeleteVersion(ByVal sender As Object, ByVal e As Integer) Handles wf.DeleteVersion

            Try

                Dim cont As New HtmlTextController

                cont.DeleteHtmlText(e)

                ' keinen neuen Text binden

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try


        End Sub


#End Region

#Region " Private Methods "

        Private Sub BindId(ByVal Id As Int32)

            ' get HtmlText object
            Dim objHTML As New HtmlTextController
            Dim objText As HtmlTextInfo

            objText = objHTML.GetHtmlTextById(Id)

            If Not objText Is Nothing Then
                ' initialize control values
                teContent.Text = objText.DeskTopHTML
            Else

                If Me.UserLevel = UserLevelEnum.Editor Or Me.UserLevel = UserLevelEnum.Workflow Then
                    ' get default content from resource file
                    teContent.Text = Localization.GetString("AddContent", LocalResourceFile)
                End If

            End If

        End Sub

#End Region

    End Class

End Namespace
